"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _get = _interopRequireDefault(require("rc-util/lib/utils/get"));

var _set = _interopRequireDefault(require("rc-util/lib/utils/set"));

var _react = require("react");

var _List = require("../List");

var _excluded = ["name", "children", "ignoreFormListField"];

var ProFormDependency = function ProFormDependency(_ref) {
  var names = _ref.name,
      _children = _ref.children,
      ignoreFormListField = _ref.ignoreFormListField,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var context = (0, _react.useContext)(_proUtils.ProFormContext); // ProFromList 的 field，里面有name和key

  var formListField = (0, _react.useContext)(_List.FormListContext); // flatten each name into an (string | number)[]

  var flattenNames = (0, _react.useMemo)(function () {
    return names.map(function (itemName) {
      var _formListField$listNa;

      var name = [itemName]; // ignoreFormListField为 true 或 formListField.name === undefined 时
      // 应从全局取值，要将 names 中各项的路径前缀(formListField.listName)忽略

      if (!ignoreFormListField && formListField.name !== undefined && ((_formListField$listNa = formListField.listName) === null || _formListField$listNa === void 0 ? void 0 : _formListField$listNa.length)) {
        name.unshift(formListField.listName);
      }

      return name.flat(1);
    });
  }, [formListField.listName, formListField.name, ignoreFormListField, names]);
  return (0, _jsxRuntime.jsx)(_antdV.Form.Item, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, rest), {}, {
    noStyle: true,
    shouldUpdate: function shouldUpdate(prevValues, nextValues, info) {
      if (typeof rest.shouldUpdate === 'boolean') {
        return rest.shouldUpdate;
      } else if (typeof rest.shouldUpdate === 'function') {
        var _rest$shouldUpdate;

        return (_rest$shouldUpdate = rest.shouldUpdate) === null || _rest$shouldUpdate === void 0 ? void 0 : _rest$shouldUpdate.call(rest, prevValues, nextValues, info);
      }

      return flattenNames.some(function (name) {
        return !(0, _proUtils.isDeepEqualReact)((0, _get.default)(prevValues, name), (0, _get.default)(nextValues, name));
      });
    },
    children: function children(form) {
      var values = {};

      for (var i = 0; i < names.length; i++) {
        var _context$getFieldForm;

        var pathToGet = flattenNames[i],
            pathToSet = names[i];
        var finalName = [pathToSet].flat(1);
        var value = (_context$getFieldForm = context.getFieldFormatValueObject) === null || _context$getFieldForm === void 0 ? void 0 : _context$getFieldForm.call(context, pathToGet);

        if (value && Object.keys(value).length) {
          // transform 会生成多余的value，这里需要注入一下
          values = (0, _proUtils.merge)({}, values, value);

          if ((0, _get.default)(value, pathToGet)) {
            values = (0, _set.default)(values, finalName, (0, _get.default)(value, pathToGet), false);
          }
        } else {
          var _form$getFieldValue;

          value = (_form$getFieldValue = form.getFieldValue) === null || _form$getFieldValue === void 0 ? void 0 : _form$getFieldValue.call(form, pathToGet);

          if (typeof value !== 'undefined') {
            values = (0, _set.default)(values, finalName, value, false);
          }
        }
      }

      return _children === null || _children === void 0 ? void 0 : _children(values, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), context));
    }
  }));
};

var _default = ProFormDependency;
exports.default = _default;